/**   
 * Copyright © 2018 HIT akeil. Tech Ltd. All rights reserved.
 * 
 * 功能描述：
 * @Package: com.akeil.test 
 * @author: SNOW-PC   
 * @date: 2018年6月19日 上午10:16:39 
 */
package com.akeil.flash.utils;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Namespace;
import org.dom4j.io.OutputFormat;
import org.eclipse.jface.preference.IPreferenceStore;

import com.akeil.flash.model.XmlData;
import com.akeil.flash.pojo.Data;

import flash.Activator;

public class LimsXMLMaker {
	Calendar calendar = Calendar.getInstance();
	SimpleDateFormat format0 = new SimpleDateFormat("HH:mm:ss");
	DecimalFormat df1 = new DecimalFormat("0.0");
	DecimalFormat df4 = new DecimalFormat("0.0000");
	IPreferenceStore preferenceStore = Activator.getDefault().getPreferenceStore();
	
	private String username;
	private String password;
	private String fullFileName;
	private String sampleno;
	private String anasysisMethod;
	private int methodNo;
	private String note;
	
	public LimsXMLMaker() {

	}
	
	public void outputLIMS(Data data) {
		testCreateLimisXml(readyData_GBT261_ZD(data));
	}
	
	public void outputLIMS(Data data1,Data data2) {
		switch(methodNo){
		case 0://GBT261_D
			break;
			
		case 1://GBT261
			testCreateLimisXml(readyData_GBT261_Z(data1,data2));
			break;
			
		}
	}
	
	/**
	 * 
	 * @title : readyData_GBT261_ZD
	 * @description : 为闪点自动单组数据
	 * @param data
	 * @return
	 * @return : List<XmlData>
	 * @throws :
	 */
	private  List<XmlData> readyData_GBT261_ZD(Data data){
		List<XmlData> allDatasforxml = new ArrayList<XmlData>();

		XmlData d = new XmlData();
		d.setName("观察温度间隔");// g/ml
		if(data.getPretemperature() >= 110){
			d.setText("2℃");
		}else{
			d.setText("1℃");
		}
		allDatasforxml.add(d);
		
		d = new XmlData();
		d.setName("初始点火温度");// g/ml
		d.setText(df4.format(data.getPretemperature() - 18));
		//d.setResultType("N");
		allDatasforxml.add(d);
		
		d = new XmlData();
		d.setName("现象1");// g
		d.setText("未闪火");
		//d.setResultType("O");
		allDatasforxml.add(d);
		
		d = new XmlData();
		d.setName("观察闪点温度");
		if(data.getPretemperature() >= 110){
			d.setText(df4.format(data.getFlashtemperature() - 2));
		}else{
			d.setText(df4.format(data.getFlashtemperature() - 1));
		}	
		//d.setResultType("N");
		allDatasforxml.add(d);
		
		d = new XmlData();
		d.setName("现象2");// g
		d.setText("闪火");
		//d.setResultType("O");
		allDatasforxml.add(d);
		
		d = new XmlData();
		d.setName("闪点");
		d.setText(df4.format(data.getFlashtemperature()));
		//d.setResultType("N");
		allDatasforxml.add(d);

		
		d = new XmlData();
		d.setName("备注");
		d.setText(this.note);
		//d.setResultType("T");
		allDatasforxml.add(d);
		
		return allDatasforxml;
	}
	
	/**
	 * 
	 * @title : readyData_GBT261_Z
	 * @description : 为闪点自动双组数据
	 * @param data1
	 * @param data2
	 * @return
	 * @return : List<XmlData>
	 * @throws :
	 */
	private  List<XmlData> readyData_GBT261_Z(Data data1,Data data2){
		List<XmlData> allDatasforxml = new ArrayList<XmlData>();
		//第一组
		XmlData d = new XmlData();
		d.setName("观察温度间隔");// g/ml
		if(data1.getPretemperature() >= 110){
			d.setText("2℃");
		}else{
			d.setText("1℃");
		}
		allDatasforxml.add(d);
		
		d = new XmlData();
		d.setName("初始点火温度1");// g/ml
		d.setText(df4.format(data1.getPretemperature() - 18));
		//d.setResultType("N");
		allDatasforxml.add(d);
		
		d = new XmlData();
		d.setName("现象1");// g
		d.setText("未闪火");
		//d.setResultType("O");
		allDatasforxml.add(d);
		
		d = new XmlData();
		d.setName("观察闪点温度1");
		if(data1.getPretemperature() >= 110){
			d.setText(df4.format(data1.getFlashtemperature() - 2));
		}else{
			d.setText(df4.format(data1.getFlashtemperature() - 1));
		}	
		//d.setResultType("N");
		allDatasforxml.add(d);
		
		d = new XmlData();
		d.setName("现象2");// g
		d.setText("闪火");
		//d.setResultType("O");
		allDatasforxml.add(d);
		
		d = new XmlData();
		d.setName("闪点1");
		d.setText(df4.format(data1.getFlashtemperature()));
		//d.setResultType("N");
		allDatasforxml.add(d);

		//第二组
		d = new XmlData();
		d.setName("初始点火温度2");// g/ml
		d.setText(df4.format(data2.getPretemperature() - 18));
		//d.setResultType("N");
		allDatasforxml.add(d);
		
		d = new XmlData();
		d.setName("现象3");// g
		d.setText("未闪火");
		//d.setResultType("O");
		allDatasforxml.add(d);
		
		d = new XmlData();
		d.setName("观察闪点温度2");
		if(data2.getPretemperature() >= 110){
			d.setText(df4.format(data2.getFlashtemperature() - 2));
		}else{
			d.setText(df4.format(data2.getFlashtemperature() - 1));
		}	
		//d.setResultType("N");
		allDatasforxml.add(d);
		
		d = new XmlData();
		d.setName("现象4");// g
		d.setText("闪火");
		//d.setResultType("O");
		allDatasforxml.add(d);
		
		d = new XmlData();
		d.setName("闪点2");
		d.setText(df4.format(data2.getFlashtemperature()));
		//d.setResultType("N");
		allDatasforxml.add(d);
		
		//other
		d = new XmlData();
		d.setName("备注");
		d.setText(this.note);
		//d.setResultType("T");
		allDatasforxml.add(d);
		
		return allDatasforxml;
	}	
	
	
	/*
	 * List<XmlData> datas : 试验过程数据
	 * Map<String, Object> dataMap : 试验条件数据
	 */
	//public void testCreateQiyouXml(Map<String, Object> dataMap) {
	public void testCreateLimisXml(List<XmlData> datas) {
		Element entity_;
		Element actions_;
		Element fields_;
		Element children_;
		Element field_name;
		Element field_text;
		Element field_result;
		// 创建一个xml文档
		Document doc = DocumentHelper.createDocument();
		
		//doc.getRootElement().asXML();
		//Namespace namespace_ = Namespace.get("http://www.thermo.com/informatics/xmlns/limsml/1.0");
			
		// 向xml文件中添加注释
		//doc.addComment("这里是注释");
		// 创建一个名为students的节点，因为是第一个创建，所以是根节点,再通过doc创建一个则会报错。
		Element root = doc.addElement("limsml","http://www.thermo.com/informatics/xmlns/limsml/1.0");
		Namespace namespace = root.getNamespace();
		root.addAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
		root.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
		
		Element headerEl = root.addElement("header");
		Element parameterEl = headerEl.addElement("parameter");
		parameterEl.addAttribute("name", "USER");
		parameterEl.addText(username);
		
		parameterEl = headerEl.addElement("parameter");
		parameterEl.addAttribute("name", "PASSWORD");
		parameterEl.addText(password);
		
		Element bodyEl = root.addElement("body");
			Element transactionEl = bodyEl.addElement("transaction");
				Element systemEl = transactionEl.addElement("system");
				systemEl.addAttribute("response_type", "system");
		
					Element entityEl = systemEl.addElement("entity");
					entityEl.addAttribute("type", "SAMPLE");
		
						Element actionsEl = entityEl.addElement("actions");
							Element actionEl = actionsEl.addElement("action");
								Element commandEl = actionEl.addElement("command");
								commandEl.addText("RESULT_ENTRY");
		
						Element fieldsEl = entityEl.addElement("fields");
							Element fieldEl = fieldsEl.addElement("field");
							fieldEl.addAttribute("id", "ID_NUMERIC");
							fieldEl.addAttribute("direction", "in");
							fieldEl.addText(sampleno);//样品编号
		
						Element childrenEl = entityEl.addElement("children");
							Element childEntityEl = childrenEl.addElement("entity");
							childEntityEl.addAttribute("type", "TEST");
		
								Element childrenFieldsEl = childEntityEl.addElement("fields");
									Element childChildrenFieldEl = childrenFieldsEl.addElement("field");
									childChildrenFieldEl.addAttribute("id", "ANALYSIS");
									childChildrenFieldEl.addAttribute("direction", "in");
									childChildrenFieldEl.addText(anasysisMethod);//分析方法
								
								// children
								Element childChildrenEl = childEntityEl.addElement("children");
			
									// 试验结果
									for (XmlData data : datas) {
										entity_ = childChildrenEl.addElement("entity");
										entity_.addAttribute("type", "RESULT");
									
											//actions_ = entity_.addElement("actions");
											fields_ = entity_.addElement("fields");
											//children_ = entity_.addElement("children");
										
												field_name = fields_.addElement("field");
												field_name.addAttribute("id", "NAME");
												field_name.addAttribute("direction", "in");
												field_name.addText(data.getName());
												
												field_text = fields_.addElement("field");
												field_text.addAttribute("id", "TEXT");
												field_text.addAttribute("direction", "in");
												field_text.addText(data.getText());
												
												if(data.getResultType() != null){
													field_result = fields_.addElement("field");
													field_result.addAttribute("id", "RESULT_TYPE");
													field_result.addAttribute("direction", "in");
													field_result.addText(data.getResultType());
													}
									}
		
		
		// 用于格式化xml内容和设置头部标签
		OutputFormat format = OutputFormat.createPrettyPrint();
		// 设置xml文档的编码为utf-8
		//format.setEncoding("utf-8");
		Writer out;
		try {
			// 创建一个输出流对象
			//out = new FileWriter((String)dataMap.get("filename"));
			
			BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (fullFileName,false),"UTF-8"));
			
			// 创建一个dom4j创建xml的对象
			//XMLWriter writer = new XMLWriter(out, format);
			
			
			// 调用write方法将doc文档写到指定路径
			writer.write(doc.asXML());
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getFullFileName() {
		return fullFileName;
	}

	public void setFullFileName(String fullFileName) {
		this.fullFileName = fullFileName;
	}

	public String getSampleno() {
		return sampleno;
	}

	public void setSampleno(String sampleno) {
		this.sampleno = sampleno;
	}

	public String getAnasysisMethod() {
		return anasysisMethod;
	}

	public void setAnasysisMethod(String anasysisMethod) {
		this.anasysisMethod = anasysisMethod;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public int getMethodNo() {
		return methodNo;
	}

	public void setMethodNo(int methodNo) {
		this.methodNo = methodNo;
	}
}
